package com.snakegame.ui;

import com.snakegame.core.GameManager;
import com.snakegame.config.GameMode;
import com.snakegame.config.GameState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 模式选择面板，允许用户选择游戏模式
 */
public class ModeSelectionPanel extends JPanel {
    private GameManager gameManager;
    private GameFrame gameFrame;
    
    public ModeSelectionPanel(GameManager gameManager, GameFrame gameFrame) {
        this.gameManager = gameManager;
        this.gameFrame = gameFrame;
        
        setLayout(new BorderLayout());
        setBackground(Color.BLACK);
        
        // 标题
        JLabel titleLabel = new JLabel("选择游戏模式");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        add(titleLabel, BorderLayout.NORTH);
        
        // 模式选择区域
        JPanel modesPanel = new JPanel();
        modesPanel.setLayout(new GridLayout(0, 1, 10, 10));
        modesPanel.setBackground(Color.BLACK);
        modesPanel.setBorder(BorderFactory.createEmptyBorder(50, 100, 50, 100));
        
        ButtonGroup modeGroup = new ButtonGroup();
        
        // 单人模式
        JRadioButton singleModeButton = new JRadioButton("单人模式");
        singleModeButton.setFont(new Font("宋体", Font.PLAIN, 18));
        singleModeButton.setForeground(Color.WHITE);
        singleModeButton.setBackground(Color.BLACK);
        singleModeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameMode(GameMode.SINGLE_PLAYER);
            }
        });
        
        // 双人模式
        JRadioButton multiModeButton = new JRadioButton("双人模式");
        multiModeButton.setFont(new Font("宋体", Font.PLAIN, 18));
        multiModeButton.setForeground(Color.WHITE);
        multiModeButton.setBackground(Color.BLACK);
        multiModeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameMode(GameMode.MULTI_PLAYER);
            }
        });
        
        // 根据当前模式设置默认选中
        if (gameManager.getGameMode() == GameMode.SINGLE_PLAYER) {
            singleModeButton.setSelected(true);
        } else {
            multiModeButton.setSelected(true);
        }
        
        modeGroup.add(singleModeButton);
        modeGroup.add(multiModeButton);
        
        modesPanel.add(singleModeButton);
        modesPanel.add(multiModeButton);
        
        // 说明文字
        JLabel instructionLabel = new JLabel("控制说明:");
        instructionLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        instructionLabel.setForeground(Color.WHITE);
        instructionLabel.setHorizontalAlignment(JLabel.LEFT);
        modesPanel.add(instructionLabel);
        
        JLabel singleControlLabel = new JLabel("单人模式: 方向键控制蛇");
        singleControlLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        singleControlLabel.setForeground(Color.GRAY);
        singleControlLabel.setHorizontalAlignment(JLabel.LEFT);
        modesPanel.add(singleControlLabel);
        
        JLabel multiControlLabel = new JLabel("双人模式: 玩家1-方向键, 玩家2-WASD");
        multiControlLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        multiControlLabel.setForeground(Color.GRAY);
        multiControlLabel.setHorizontalAlignment(JLabel.LEFT);
        modesPanel.add(multiControlLabel);
        
        add(modesPanel, BorderLayout.CENTER);
        
        // 底部按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBackground(Color.BLACK);
        
        // 返回按钮
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        buttonPanel.add(backButton);
        
        // 确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        buttonPanel.add(confirmButton);
        
        add(buttonPanel, BorderLayout.SOUTH);
    }
}    